import java.util.Scanner;
import java.util.Stack;

public class Test2 {
    // 两个字符串的最小距离
    public static void main03(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            String[] str=new String[a];
            String str1= in.next();
            String str2= in.next();
            Stack<String> stack=new Stack<>();
            for (int i = 0; i < a; i++) {
                str[i]=in.next();
                stack.add(str[i]);
            }
            int count=a-1;
            int m=-1;
            int n=-1;
            int sum=-1;
            while(!stack.isEmpty()){
                if(stack.peek().equals(str1)){
                    m=count;
                }
                if(stack.peek().equals(str2)){
                    n=count;
                }
                if(m!=-1&&n!=-1){
                    if(m-n>0){
                        if(sum > m-n){
                            sum=m-n;
                        }
                    }else{
                        if(sum > n-m){
                            sum=n-m;
                        }
                    }

                }
                stack.pop();
                count--;
            }
            if(n==-1 || m==-1){
                System.out.println(-1);
            }else{
                if(sum>0){
                    System.out.println(sum);
                }else{
                    System.out.println(-sum);
                }
            }
        }
    }







    // 最小花费爬楼梯
    public static void main02(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int a = in.nextInt();
            int[] cost=new int[a];
            for (int i = 0; i < a; i++) {
                cost[i]= in.nextInt();
            }
            int count=0;
            int sum=0;
            while(count < a-4 || count <a-5){
                if(count == 0) {
                    if (cost[count] < cost[count + 1]) {
                        sum += cost[count];
                    } else {
                        sum += cost[count + 1];
                        count++;
                    }
                }
                //count 表示所在楼梯下标
                if(cost[count+1] < cost[count+2]){
                    sum+=cost[count+1];
                    count+=1;
                }else{
                    sum+=cost[count+2];
                    count+=2;
                }
            }
            if(count == a-3){
                if(cost[count+1]< cost[count+2]+cost[count]){
                    sum+=cost[count+1];
                }else{
                    sum+=cost[count]+cost[count+2];
                }
            }
            if(count== a-4){
                if(cost[count+1]+cost[count+3] < cost[count+2]){
                    sum+=cost[count+1]+cost[count+3];
                }else{
                    sum+=cost[count+2];
                }
            }
            if(count== a-5){
                if(cost[count+1]+cost[count+3] < cost[count+2]+cost[count+4]){
                    sum+=cost[count+1]+cost[count+3];
                }else{
                    sum+=cost[count+2]+cost[count+4];
                }
            }
            System.out.println(sum);
        }
    }




    // 计算牛牛快递费用
    public static void main01(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            float a = in.nextFloat();
            String b = in.nextLine();
            int sum=0;
            if(a<=1.0){
                sum=20;
                if(b.equals(" y")){
                    sum+=5;
                }
                System.out.println(sum);
                break;
            }
            if(b.equals(" y")){
                sum+=5;
            }
            float add = a - 1;
            if(add % 1 ==0){
                sum += 20 + (int)add;
            }else{
                sum += 20 + (int)add+1;
            }
            System.out.println(sum);
        }
    }
}
